#!/usr/bin/env bash
DEPLOY_DIR=`pwd`
SERVER_NAME=`basename ${DEPLOY_DIR}`
DEPLOY_DIR=`pwd`
STDOUT_FILE=${DEPLOY_DIR}/logs/stdout.log
WAR_FILE=${DEPLOY_DIR}/cm-interface.jar

PIDS=`ps -f | grep java | grep "${DEPLOY_DIR}" `
if [ -n "$PIDS" ]; then
    kill -9 $PIDS
	echo "kill the $PIDS"
    exit 1
fi

LOGS_DIR=$DEPLOY_DIR/logs
if [ ! -d $LOGS_DIR ]; then
    mkdir $LOGS_DIR
fi

BITS=`java -version 2>&1 | grep -i 64-bit`
if [ -n "$BITS" ]; then
    JAVA_OPTS=" -server -Xmx1g -Xms256m "
else
    JAVA_OPTS=" -server -Xmx1g -Xms256m "
fi
echo -e "Starting the ${SERVER_NAME} ...\c"
nohup java $JAVA_OPTS -jar ${WAR_FILE} > $STDOUT_FILE 2>&1 &
#COUNT=0
#while [ $COUNT -lt 1 ]; do
#    echo -e ".\c"
#    sleep 1
#    COUNT=`ps -f | grep java | grep "${DEPLOY_DIR}" `
#    if [ $COUNT -gt 0 ]; then
#    	echo "start ok!"
#        break
#    fi
#done

echo "OK!"
PIDS=`ps -f | grep java | grep "${DEPLOY_DIR}" `
echo "PID: $PIDS"
echo "STDOUT: $STDOUT_FILE"